package com.smarthome.ipc.command;

import org.springframework.stereotype.Service;

import com.smarthome.domain.ipc.IpcCodeEnum;
import com.smarthome.domain.ipc.IpcConstants;
import com.smarthome.domain.ipc.IpcResponse;
import com.smarthome.domain.ipc.req.HkIpcResidenceDoorOpRequest;
import com.smarthome.domain.ipc.req.HkIpcResidenceTalkDoorRequest;
import com.smarthome.common.tools.BossCachedKeysUtil;
import com.smarthome.ipc.hk.HKMainService;

import com.smarthome.ipc.hk.entity.NetDvrLoginV30Entity;

/**
 * 海康门口机语音通信服务类
 * 
 * @author add by denghw 2015/09/23
 *
 */
@Service(IpcConstants.IPC_COMMAND_RESIDENCE_DOOR_TALK_HK)
public class HkNetVoiceTalk extends HKMainService {
	@Override
	protected void doCmd(HkIpcResidenceDoorOpRequest request, IpcResponse<?> response) {
		// TODO Auto-generated method stub
		// 1.获取语言请求对象
		HkIpcResidenceTalkDoorRequest sprequest = (HkIpcResidenceTalkDoorRequest) request;
		// 2.获取唯一的操作句柄
		NetDvrLoginV30Entity clickObj = (NetDvrLoginV30Entity) localCache.get(BossCachedKeysUtil
				.getKey(BossCachedKeysUtil.MODULE_HK_TALKDOOR_DEVICE, sprequest.getIp() + sprequest.getDeviceId()));
		if (IpcConstants.IPC_COMMAND_RESIDENCE_DOOR_TALK_HK_START.equals(sprequest.getCommand())) {
			// 3.启动语言对讲
			clickObj.initVoiceTalk();
			if (!clickObj.startVoiceTalk(sprequest)) {
				rtnBackErrMsg(sprequest.getDeviceId(), sprequest.getCommand(),
						String.valueOf(clickObj.gethCNetSDK().NET_DVR_GetLastError()), "启动语音对讲失败");
				response.setIpcCodeObj(IpcCodeEnum.SYSTEM_ERROR);
				return;
			}
		} else if (IpcConstants.IPC_COMMAND_RESIDENCE_DOOR_TALK_HK_STOP.equals(sprequest.getCommand())) {
			// 3.关闭语言对讲
			if (!clickObj.stopVoiceTalk()) {
				rtnBackErrMsg(sprequest.getDeviceId(), sprequest.getCommand(),
						String.valueOf(clickObj.gethCNetSDK().NET_DVR_GetLastError()), "关闭语音对讲失败");
				response.setIpcCodeObj(IpcCodeEnum.SYSTEM_ERROR);
				return;
			}
		} else {
			rtnBackErrMsg(sprequest.getDeviceId(), sprequest.getCommand(), IpcCodeEnum.COMMAND_NOT_FOUND.getCode(),
					IpcCodeEnum.COMMAND_NOT_FOUND.getMsg());
			response.setIpcCodeObj(IpcCodeEnum.COMMAND_NOT_FOUND);
			return;
		}
		// 4.成功
		response.setIpcCodeObj(IpcCodeEnum.SYSTEM_SUCCESS);
	}

}
